from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, JSON
from sqlalchemy.orm import relationship
import datetime
from backend.database.database import Base

class WingknifeProcess(Base):
    """影刀流程配置表"""
    __tablename__ = "wingknife_processes"

    id = Column(Integer, primary_key=True, index=True)
    process_define_id = Column(String(100), nullable=False, comment="流程定义ID")
    name = Column(String(100), nullable=False, comment="流程名称")
    description = Column(Text, nullable=True, comment="流程描述")
    # config = Column(JSON, nullable=False, comment="流程配置JSON")
    parameters = Column(JSON, nullable=False, comment="运行参数")
    output_file_path = Column(String(255), nullable=False, comment="输出文件路径")
    created_at = Column(DateTime, default=datetime.datetime.now, comment="创建时间")
    updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment="更新时间")
    created_by = Column(String(50), nullable=False, comment="创建用户")

    # 关联执行记录
    execution_records = relationship("WingknifeExecutionRecord", back_populates="process")

class WingknifeExecutionRecord(Base):
    """影刀流程执行记录表"""
    __tablename__ = "wingknife_execution_records"

    id = Column(Integer, primary_key=True, index=True)
    process_id = Column(Integer, ForeignKey("wingknife_processes.id"), nullable=False, comment="流程ID")
    run_id = Column(String(100), nullable=False, comment="运行ID")
    parameters = Column(JSON, nullable=False, comment="运行参数")
    status = Column(String(20), nullable=False, comment="状态：pending/running/completed/failed")
    result = Column(JSON, nullable=True, comment="执行结果")
    started_at = Column(DateTime, default=datetime.datetime.now, comment="开始时间")
    completed_at = Column(DateTime, nullable=True, comment="完成时间")
    created_by = Column(String(50), nullable=True, comment="创建者")
    created_at = Column(DateTime, default=datetime.datetime.now, comment="创建时间")

    # 关联流程
    process = relationship("WingknifeProcess", back_populates="execution_records") 